package com.guanyong.services;

import com.guanyong.dao.IVideoScoreDaoMapper;
import com.guanyong.model.VideoScoreModel;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * Created by mxk on 2017/6/2.
 */
@Service
public class VideoScoreService {

    @Autowired
    IVideoScoreDaoMapper iVideoScoreDaoMapper;

    //修改分数
    public ResultObject<Double> UpdateVideoScore(VideoScoreModel videoScoreModel,
                                    IObjectUpdateScore iObjectUpdateScore,
                                    String objectid){
        ResultObject<Double> resultObject=new ResultObject<Double>();
        Integer res = iVideoScoreDaoMapper.update(videoScoreModel);
        if (res>-1){
            Double sumScore=0.0;
            int temp=0;
            if (videoScoreModel.getfDouban()>0){
                sumScore+=videoScoreModel.getfDouban();
                temp++;
            }
            if (videoScoreModel.getfImdb()>0){
                sumScore+=videoScoreModel.getfImdb();
                temp++;
            }
            if (videoScoreModel.getfLanfanqie()>0){
                sumScore+=videoScoreModel.getfLanfanqie();
                temp++;
            }
            double score=0.0;
            if (temp==0){
                sumScore=sumScore/temp;
                BigDecimal bg = new BigDecimal(sumScore).setScale(2, RoundingMode.UP);
                score=bg.doubleValue();
            }
            iObjectUpdateScore.UpdateObjectScore(objectid,score);
            resultObject.setCode(ResultCode.OperateOk);
            resultObject.setResult(score);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }


    public VideoScoreModel  GetVideoScoreByObjectId(Integer classType,String objectid){
        return iVideoScoreDaoMapper.selectVideoScore(objectid,classType);
    }

}
